package org.jivesoftware.webchat.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.infoview.db.DbUtil;

public class LeaveMessage extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public LeaveMessage() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request,response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
        
		PrintWriter out = response.getWriter();
		response.setContentType("text/html; charset=gb2312");
        request.setCharacterEncoding("gb2312");
        
        final String phone = request.getParameter("phone");
        final String username=request.getParameter("username");
		final String mobilephone=request.getParameter("mobilephone");
		final String email= request.getParameter("email");
		final String title =request.getParameter("title");
		final String content =request.getParameter("content");
		final String chs=request.getParameter("chs");
		if(phone==null||phone==""||email==null||email==""||title==null||title==""||content==null||content==""||username==null||username==""){
			String path=request.getContextPath();
			response.sendRedirect(path+"/error.jsp");
			return;
		}
		System.out.println("username="+username);
		System.out.println("email="+email);
		System.out.println("title="+title);
		System.out.println("content="+content);
		
		Connection conn=null;
		PreparedStatement pstmt=null;
		
		try{
			
			conn=DbUtil.getConnection();
			String sql="insert into ofmessage(title,content,phone,email,leaveTime,status,username,telephone,replayWay) values(?,?,?,?,?,?,?,?,?)";
			pstmt=conn.prepareStatement(sql);
			Date date=new Date();
			SimpleDateFormat sf=new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
			String dates=sf.format(date);
			pstmt.setString(1, title);
			pstmt.setString(2, content);
			pstmt.setString(3, phone);
			pstmt.setString(4, email);
			pstmt.setString(5, dates);
			pstmt.setString(6, "0");
			pstmt.setString(7,username);
			pstmt.setString(8,mobilephone);
			pstmt.setString(9,chs);
			pstmt.execute();
			
		}catch(SQLException e){
			e.printStackTrace();
			
		}
		finally{
			if(pstmt!=null){
				try{
					pstmt.close();
				}catch(SQLException e){
					pstmt=null;
				}
			}
			if(conn!=null){
				try{
					conn.close();
				}catch(SQLException e){
					conn=null;
				}
			}
			
		}
		String str=request.getContextPath();
		response.sendRedirect(str+"/chat-ended.jsp");
		return;
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
